<table width="100%"><tr><td width="20%">
<a href="../index.html">&lt; index</a><br />
<a href="index.html">&lt; 8. All purposes container</a><br />
<a href="list_array.html">&lt; 8.2 Basic array operations</a>
</td><td width="60%">
<p align="center">
=====================================<br />
8.3 Basic list operations<br />
=====================================<br />
</p></td><td width="20%">
<a href="list_stack.html">&gt; 8.4 Basic stack operations</a>
</td></tr></table>
<style type="text/css">
.code { color:#444444; background-color:#EEEEEE;}
pre { color:#444444; background-color:#EEEEEE;}
table.param td { border : 1px solid #000000; }
th { background-color: #44BBFF; color: #FFFFFF }
table.none td { border : 0 }
</style>
You can insert a value inside the list.<br />
<p class="code"><pre>
C++ : template &lt;class T&gt; void TCODList::insertBefore(const T elt,int before)
C   : void TCOD_list_insert_before(TCOD_list_t l,const void *elt,int before)
</pre></p>
<table class="param">
<tr><th>Parameter</th><th>Description</th></tr>
<tr><td>elt</td><td>Element to insert in the list.</td></tr>
<tr><td>idx</td><td>Index of the element after the insertion.<br />0 &lt;= idx &lt; list size</td></tr>
<tr><td>l</td><td>In the C version, the list handler, returned by <a href="list_init.html">a constructor</a>.</td></tr>
</table>
<br />
Example :<br />
<p class="code"><pre>
C++ : TCODList&lt;int&gt; intList; // the list is empty (contains 0 elements)
      intList.set(0,5); // the list contains 1 element at position 0, value = 5
      intList.insertBefore(2,0); // the list contains 2 elements : 2,5
C   : TCOD_list_t intList = TCOD_list_new();
      TCOD_list_set(intList,0,(const void *)5);
      TCOD_list_insert_before(intList,(const void *)2,0);
</pre></p><br /><hr />
You can remove an element from the list.<br />
<p class="code"><pre>
C++ : template &lt;class T&gt; void TCODList::remove(const T elt)
      template &lt;class T&gt; void TCODList::removeFast(const T elt)
C   : void TCOD_list_remove(TCOD_list_t l, const void * elt)
      void TCOD_list_remove_fast(TCOD_list_t l, const void * elt)
</pre></p>
<table class="param">
<tr><th>Parameter</th><th>Description</th></tr>
<tr><td>elt</td><td>The element to remove</td></tr>
<tr><td>l</td><td>In the C version, the list handler, returned by <a href="list_init.html">a constructor</a>.</td></tr>
</table>
The _fast versions replace the element to remove with the last element of the list. They're faster, but do not preserve the list order.
<br />
Example :<br />
<p class="code"><pre>
C++ : TCODList&lt;int&gt; intList; // the list is empty (contains 0 elements)
      intList.set(0,5); // the list contains 1 element at position 0, value = 5
      intList.remove(5); // the list is empty
C   : TCOD_list_t intList = TCOD_list_new();
      TCOD_list_set(intList,0,(const void *)5);
      TCOD_list_remove(intList,(const void *)5);
</pre></p><br /><hr />
You can concatenate two lists. Every element of l2 will be added to current list (or l in the C version) :<br />
<p class="code"><pre>
C++ : template &lt;class T&gt; void TCODList::addAll(const TCODList<T> &l2)
C   : void TCOD_list_add_all(TCOD_list_t l, TCOD_list_t l2)
</pre></p>
<table class="param">
<tr><th>Parameter</th><th>Description</th></tr>
<tr><td>l</td><td>The list inside which elements will be added.</td></tr>
<tr><td>l2</td><td>the list handler containing elements to insert.</td></tr>
</table>
<br />
Example :<br />
<p class="code"><pre>
C++ : TCODList&lt;int&gt; intList;
      intList.set(1,3); // intList contains 2 elements : 0, 3
      TCODList&lt;int&gt; intList2; // intList2 is empty
      intList2.set(0,1); // intList2 contains 1 element : 1
      intList2.addAll(intList); // intList2 contains 3 elements : 1, 0, 3
C   : TCOD_list_t intList = TCOD_list_new();
      TCOD_list_set(intList,1,(const void *)3);
      TCOD_list_t intList2 = TCOD_list_new();
      TCOD_list_set(intList2,0,(const void *)1);
      TCOD_list_add_all(intList2,intList);
</pre></p>
<br /><hr />
You can empty a list with clear :<br />
<p class="code"><pre>
C++ : template &lt;class T&gt; void TCODList::clear()
C   : void TCOD_list_clear(TCOD_list_t l)
</pre></p>
<table class="param">
<tr><th>Parameter</th><th>Description</th></tr>
<tr><td>l</td><td>In the C version, the list handler, returned by <a href="list_init.html">a constructor</a>.</td></tr>
</table>
<br />
Example :<br />
<p class="code"><pre>
C++ : TCODList&lt;int&gt; intList;
      intList.set(0,3); // intList contains 1 element
      intList.clear(); // intList is empty
C   : TCOD_list_t intList = TCOD_list_new();
      TCOD_list_set(intList,0,(const void *)5);
      TCOD_list_clear(intList);
</pre></p>
<br /><hr />
For lists containing pointers, you can clear the list and delete (free) the elements :<br />
<p class="code"><pre>
C++ : template &lt;class T&gt; void TCODList::clearAndDelete()
C   : void TCOD_list_clear_and_delete(TCOD_list_t l)
</pre></p>
<table class="param">
<tr><th>Parameter</th><th>Description</th></tr>
<tr><td>l</td><td>In the C version, the list handler, returned by <a href="list_init.html">a constructor</a>.</td></tr>
</table>
<br />
Example :<br />
<p class="code"><pre>
C++ : TCODList&lt;MyClass *&gt; intList;
      MyClass * cl=new MyClass(); // new instance of MyClass allocated here
      intList.set(0,cl); 
      intList.clear(); // the list is empty. cl is always valid
      intList.set(0,cl); 
      intList.clearAndDelete(); // the list is empty. delete cl has been called. The address cl is no longer valid.
C   : TCOD_list_t intList = TCOD_list_new();
      void *data=calloc(10,1); /* some memory allocation here */
      TCOD_list_set(intList,0,(const void *)data);
      TCOD_list_clear(intList); /* the list is empty, but data is always valid */
      TCOD_list_set(intList,0,(const void *)data);
      TCOD_list_clear_and_delete(intList); /* the list is empty, free(data) has been called. The address data is no longer valid */
</pre></p>



